package cn.ljy.authority.web.controller.backstage.authority;


import cn.ljy.authority.model.SysRole;
import cn.ljy.authority.model.param.RoleEnabledParam;
import cn.ljy.authority.model.param.RolePermissionParam;
import cn.ljy.authority.model.query.SysRoleQuery;
import cn.ljy.authority.service.SysRoleService;
import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.model.result.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api(tags = "后台角色管理模块")
@PreAuthorize("hasAuthority('sys:role')")
@RestController
@RequestMapping(value = "/backstage/authority/role")
public class SysRoleController extends BaseBackstageController<SysRole,SysRoleQuery,Integer> {

    @Autowired
    private SysRoleService baseService;

    @Override
    public String getAuthorityPrefix() {
        return "sys:role"; // 定义模块权限前缀
    }

//    @GetMapping("/all")
//    @Override
//    public ResponseData query(SysRoleQuery sysRoleQuery){
//        return super.query(sysRoleQuery);
//    }
//
//    @ApiOperation(value = "分页查询角色列表")
//    @PreAuthorize("hasAuthority('sys:role:query')")
//    @Override
//    public ResponseData query(Integer page, Integer pageSize, SysRoleQuery sysRoleQuery){
//        return super.query(page,pageSize,sysRoleQuery);
//    }
//
//
//    @ApiOperation(value = "批量删除指定的多个角色")
//    @PreAuthorize("hasAuthority('sys:role:del')")
//    @Override
//    public ResponseData deletes(@NotEmpty(message="请选择要移除的记录")Integer[] ids){
//        return super.deletes(ids);
//    }
//
//    @ApiOperation(value = "强制批量删除指定的多个角色")
//    @PreAuthorize("hasAuthority('sys:role:del:force')")
//    @Override
//    public ResponseData forceDeletes(@NotEmpty(message="请选择要移除的记录")Integer[] ids){
//        return super.forceDeletes(ids);
//    }
//
//    @ApiOperation(value = "读取指定角色信息")
//    @PreAuthorize("hasAuthority('sys:role:query')")
//    @Override
//    public ResponseData get(@PathVariable Integer id) {
//        return super.get(id);
//    }
//
//    @ApiOperation(value = "编辑角色")
//    @PreAuthorize("hasAuthority('sys:role:edit')")
//    @Override
//    public ResponseData edit(@Validated({UpdateWorkValidation.class}) @RequestBody SysRole sysRole){
//        return super.edit(sysRole);
//    }
//
//    @ApiOperation(value = "添加角色")
//    @PreAuthorize("hasAuthority('sys:role:add')")
//    @Override
//    protected ResponseData add(@Validated({AddWorkValidation.class}) @RequestBody SysRole sysRole) {
//       return super.add(sysRole);
//    }


    //=========================扩展部分=============================================//

    @ApiOperation(value = "批量更改指定的多个角色的可用属性")
    @PreAuthorize("hasAuthority('sys:role:edit')")
    @PatchMapping("/edit/enabled")
    public ResponseData updateEnabled(@Valid @RequestBody RoleEnabledParam roleEnabledParam){
        baseService.updateEnabledByIdIn(roleEnabledParam.getIds(),roleEnabledParam.getEnabled());
        return ResponseData.success(null);
    }

    //======================================权限操作=====================================================//
    @ApiOperation(value = "获取角色已有的菜单id")
    @PreAuthorize("hasAuthority('sys:role:permission:query')")
    @GetMapping("/permission/{roleId}")
    protected ResponseData permissionManage(@PathVariable @NotNull(message="roleId参数不能未空") Integer roleId) {
        return ResponseData.success(baseService.getPermissionIdsByRoleId(roleId));
    }

    @ApiOperation(value = "角色的权限分配")
    @PreAuthorize("hasAuthority('sys:role:permission:edit')")
    @PutMapping("/permission")
    protected ResponseData permissionManage(@Valid @RequestBody RolePermissionParam rolePermissionParam) {
        baseService.updateRolePermission(rolePermissionParam.getRoleId(),rolePermissionParam.getPermissionIds());
        return ResponseData.success(null);
    }
}
